using System;
using System.Collections.Generic;

namespace shfa.snomtastic.data.business
{
    public class UserComparer : IComparer<UserInfo>
    {
        private string _sortExpression;
        public string SortExpression
        {
            get { return _sortExpression; }
            set { _sortExpression = value; }
        }

		private string _sortDirection;
        public string SortDirection
        {
            get { return _sortDirection; }
            set { _sortDirection = value; }
        }

        public UserComparer(string sortExpression)
        {
            int iSplitPos = sortExpression.IndexOf(" ");
            SortExpression = (iSplitPos > 0) ? sortExpression.Substring(0, iSplitPos) : sortExpression;
            SortDirection = (iSplitPos > 0) ? sortExpression.Substring(iSplitPos + 1).ToUpper() : "ASC";
        }

        public UserComparer(string sortExpression, string sortDirection)
        {
            this.SortExpression = sortExpression;
            this.SortDirection = sortDirection;
        }

        int IComparer<UserInfo>.Compare(UserInfo x, UserInfo y)
        {
            UserInfo oX = (SortDirection == "ASC") ? x : y;
            UserInfo oY = (SortDirection == "ASC") ? y : x;

            switch (SortExpression)
            {				
				case "UserId":
					return oX.UserId.CompareTo(oY.UserId);				
				case "GroupId":
					return oX.GroupId.CompareTo(oY.GroupId);				
				case "UserName":
					return oX.UserName.CompareTo(oY.UserName);				
				case "Password":
					return oX.Password.CompareTo(oY.Password);				
				case "PasswordUpdate":
					return oX.PasswordUpdate.CompareTo(oY.PasswordUpdate);				
				case "PasswordUpdateAD":
					return oX.PasswordUpdateAD.CompareTo(oY.PasswordUpdateAD);				
				case "CreatedDate":
					return oX.CreatedDate.CompareTo(oY.CreatedDate);				
				case "UpdatedDate":
					return oX.UpdatedDate.CompareTo(oY.UpdatedDate);				
				case "LastAccessDate":
					return oX.LastAccessDate.CompareTo(oY.LastAccessDate);
				default:
                    return 0;
            }
        }
    }
}
